<?php

class Account extends My_Controller
{
    public function index()
    {
        
    }
    public function user_manager()
    {
             if($this->session->userdata('role') == 1 || $this->session->userdata('role') == 3 ) {
        $data['list'] = $this->m_account->get_all_account_new();
        $data['quyen'] = $this->m_account->get_all_role();
       // var_dump($data['quyen']); exit;
        $this->load->view('User/Admin_user_list',$data);
        }
        else redirect('account/login_admin');
    }
    public function add_new_account()
    {
        if($this->session->userdata('role') == 1) {
       // $ROLE_ID,$USERNAME,$PASSWORD,$EMAIL,$FULL_NAME,$FACEBOOK,$STEAM_PROFILE,$STEAMOFFER_URL,$ADDRESS,$PHONE_NUMBER,$AVATAR
        $url = $this->input->post('url1');
        $add_user = $this->m_account->add_new_account($this->input->post('role'),$this->input->post('username'),md5(md5($this->input->post('password'))),
        $this->input->post('email'),$this->input->post('fullname'),$this->input->post('facebook'),$this->input->post('steamprofile'),$this->input->post('steamurl'),$this->input->post('address')
        ,$this->input->post('phone'),$this->input->post('avatar'));
        redirect($url);
        } else redirect('account/login_admin');
    }
    function update_account()
    {
         if($this->session->userdata('role') == 1) {
        $id = $this->uri->segment(3);   
        $acc = $this->m_account->get_account_by_id($id);
        $data['quyen'] = $this->m_account->get_all_role();
        $data['acc'] = $acc;
        if($this->input->post('capnhat_user') != '' )
        { 
            $kq = $this->m_account->update_account($id,$this->input->post('role'),$this->input->post('username'),md5(md5($this->input->post('password'))),
        $this->input->post('email'),$this->input->post('fullname'),$this->input->post('facebook'),$this->input->post('steamprofile'),$this->input->post('steamurl'),$this->input->post('address')
        ,$this->input->post('phone'),$this->input->post('avatar'));
            redirect('account/update_account/'.$id);
        }
        $this->load->view('User/Admin_edit_user',$data);
         } else redirect('account/login_admin');
    }
    public function cap_nhat_wallet()
    {
        if($this->session->userdata('role') == 1) {
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $url = $this->input->post('current');
        $date = getdate();
        $ngaycapnhat = $date['year'].'-'.$date['mon'].'-'.$date['mday'];
        $id = $this->input->post('account_id');
        $wallet_moi = $this->input->post('wallet_moi');
        $kq = $this->m_account->update_wallet($id,$ngaycapnhat,$wallet_moi);
        redirect($url);
          } else redirect('account/login_admin');
    }
    public function checkusername($username)
    {
    	$this->load->model('m_account');
    	$result = $this->m_account->check_username($username);
    	echo $result;
    }

    public function register()
    {
    	$username = $this->input->post('regusername');
        $password = $this->input->post('regpw');
        $password = md5(md5($password));
        $facebookprofile = $this->input->post('facebookprofile');
        $steamprofile = $this->input->post('steamprofile');
        $email = $this->input->post('regemail');

        $this->load->model('m_account');
        $result = $this->m_account->add_new_account(2,$username,$password,$email,'',$facebookprofile,$steamprofile,'','','','',0);
        if($result)
        {
            if($this->m_account->checklogin($username, $password))
            {   
                $result = $this->m_account->get_account_by_username($username);
                $session_data = array(
                    'accountid' => $result->ACCOUNT_ID,
                    'username' => $result->USERNAME,
                    'fullname' => $result->FULL_NAME,
                    'role' => $result->ROLE_ID,
                    'wallet' => $result->WALLET
                );

                $this->session->set_userdata($session_data);
                redirect();
            }
        }
        else
            redirect();
    }

    public function login()
    {
    	$username = $this->input->post('loginusername');
        $password = $this->input->post('loginpw');
        $password = md5(md5($password));
        $this->load->model('m_account');
        if($this->m_account->checklogin($username, $password))
        {	
        	$result = $this->m_account->get_account_by_username($username);
        	$session_data = array(
        		'accountid' => $result->ACCOUNT_ID,
        		'username' => $result->USERNAME,
        		'fullname' => $result->FULL_NAME,
        		'role' => $result->ROLE_ID,
        		'wallet' => $result->WALLET
        	);

        	$this->session->set_userdata($session_data);
        	redirect();
        }
        else
        {
        	redirect();
        }
    }
    function check_login()
    {
      //  echo 'abc';
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $password = md5(md5($password));
 //       $this->load->model('m_account');
        if($this->m_account->checklogin($username, $password))
        {	
           // echo 'da vo if';
        	$result = $this->m_account->get_account_by_username($username);
        	$session_data = array(
        		'accountid' => $result->ACCOUNT_ID,
        		'username' => $result->USERNAME,
        		'fullname' => $result->FULL_NAME,
        		'role' => $result->ROLE_ID,
        		'wallet' => $result->WALLET
        	);
            	$this->session->set_userdata($session_data);
        	redirect('item/show_item_2');
        }
        else
        {
          //  echo 'fail';
        	//redirect();
        }
    }
    public function login_admin()
    {
        
        $this->load->view('admin_layout/Login');
    }

    public function logout()
    {
        $this->cart->destroy();
    	$this->session->sess_destroy();
    	redirect();
    }
    public function logout_admin()
    {
        $this->cart->destroy();
    	$this->session->sess_destroy();
    	redirect('account/login_admin');
    }

    public function myprofile()
    {
        $accountid = $this->session->userdata('accountid');
        $this->load->model('m_item');
        $this->load->model('m_account');
        $data['hero'] = $this->m_item->get_all_hero();
        $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        $data['accounts'] = $this->m_account->get_account_by_id($accountid);
        $this->load->view('layout/Myprofile', $data);
    }

    public function addbank()
    {
        $bankname = $this->input->post('bankname');
        $banknumber = $this->input->post('banknumber');
        $accountid = $this->session->userdata('accountid');

        $this->load->model('m_account');
        $result = $this->m_account->addbank($accountid, $bankname, $banknumber);

        $accountid = $this->session->userdata('accountid');
        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero();
        $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        $data['accounts'] = $this->m_account->get_account_by_id($accountid);
        $this->load->view('layout/Myprofile', $data);
    }

    public function deletebank($bankid)
    {
        $this->load->model('m_account');
        $result = $this->m_account->deletebankaccount($bankid);

        $accountid = $this->session->userdata('accountid');
        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero();
        $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        $data['accounts'] = $this->m_account->get_account_by_id($accountid);
        $this->load->view('layout/Myprofile', $data);
    }

    public function updatepassword()
    {
        $accountid = $this->session->userdata('accountid');
        $password = $this->input->post('newpass');
        $password = md5(md5($password));
        $this->load->model('m_account');
        $result = $this->m_account->changepassword($accountid, $password);

        $accountid = $this->session->userdata('accountid');
        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero();
        $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        $data['accounts'] = $this->m_account->get_account_by_id($accountid);
        $this->load->view('layout/Myprofile', $data);
    }
    
    public function updateaccount()
    {
        $accountid = $this->session->userdata('accountid');
        $Email = $this->input->post('Email');
        $Fullname = $this->input->post('Fullname');
        $Facebook = $this->input->post('Facebook');
        $Steamprofile = $this->input->post('Steamprofile');
        $SteamOffer = $this->input->post('SteamOffer');
        $Address = $this->input->post('Address');
        $PhoneNumber = $this->input->post('PhoneNumber');

        $this->load->model('m_account');
        $result =  $this->m_account->updateaccount1($accountid,$Email,$Fullname,$Facebook,$Steamprofile,$SteamOffer,$Address,$PhoneNumber);

        $this->load->model('m_item');
        $data['hero'] = $this->m_item->get_all_hero();
        $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        $data['accounts'] = $this->m_account->get_account_by_id($accountid);
        $this->load->view('layout/Myprofile', $data);
    }

}

?>